<?php 
class Core_setting extends MY_Controller {
	function __construct() {
		parent::__construct();
		$this->load->helper('file');
	}
	
	/**
	 * 网站核心设置
	 */
	function core_setting_form() {
		
		////////////////////////常量设置//////////////////////////
		$data[0]['description'] = "管理员文件夹";
		$data[0]['name'] = "ADMIN_FOLDER";
		$data[0]['value'] = ADMIN_FOLDER;
		
		////////////////////////数据库设置//////////////////////////
		$db_config_file = read_file(DATABASE_CONFIG_FILE);
		
		$hostname = "/.db\['default'\]\['hostname'\] = '(.*?)';/";
		preg_match($hostname, $db_config_file, $hostname_res);
		$data[1]['description'] = "数据库服务器地址/IP";
		$data[1]['name'] = "hostname";
		$data[1]['value'] = $hostname_res[1];
		
		$username = "/.db\['default'\]\['username'\] = '(.*?)';/";
		preg_match($username, $db_config_file, $username_res);
		$data[2]['description'] = "数据库用户名";
		$data[2]['name'] = "username";
		$data[2]['value'] = $username_res[1];
		
		$password = "/.db\['default'\]\['password'\] = '(.*?)';/";
		preg_match($password, $db_config_file, $password_res);
		$data[3]['description'] = "数据库密码";
		$data[3]['name'] = "password";
		$data[3]['value'] = $password_res[1];
		
		$database = "/.db\['default'\]\['database'\] = '(.*?)';/";
		preg_match($database, $db_config_file, $database_res);
		$data[4]['description'] = "数据库名";
		$data[4]['name'] = "database";
		$data[4]['value'] = $database_res[1];
		
		$dbdriver = "/.db\['default'\]\['dbdriver'\] = '(.*?)';/";
		preg_match($dbdriver, $db_config_file, $dbdriver_res);
		$data[5]['description'] = "数据库类型";
		$data[5]['name'] = "dbdriver";
		$data[5]['value'] = $dbdriver_res[1];
		
		/////////////////////cookie加密密钥设置///////////////////////
		$config_config_file = read_file(MAIN_CONFIG_FILE);
		
		$encryption_key = "/.config\['encryption_key'\] = '(.*?)';/";
		preg_match($encryption_key, $config_config_file, $encryption_key_res);
		$data[6]['description'] = "cookie加密密钥";
		$data[6]['name'] = "encryption_key";
		$data[6]['value'] = $encryption_key_res[1];
		//echo "<pre>";
		//print_r($data[6]);
		//return ;
		$temp['data'] = $data;
		$this->load->view('admin/core_setting/main', $temp);
	}
	
	function admin_folder_submit() {
		$constants_file = read_file(CONSTANTS_CONFIG_FILE);
		
		$admin_folder_post = $this->input->post("admin_folder_to_change");
		
		$admin_folder = "/define\('ADMIN_FOLDER', '(.*?)'\);/";
		
		if ($admin_folder_post != ADMIN_FOLDER) {
			$constants_file = preg_replace($admin_folder, "define('ADMIN_FOLDER', '" . $admin_folder_post . "');", $constants_file);
		}
		
		$file = $_SERVER['DOCUMENT_ROOT'] . '/' . SYSTEM_FOLDER . '/application/controllers/' . ADMIN_FOLDER;
		$rename = $_SERVER['DOCUMENT_ROOT'] . '/' . SYSTEM_FOLDER . '/application/controllers/' . $admin_folder_post;
		
		if (write_file(CONSTANTS_CONFIG_FILE, $constants_file) && rename($file, $rename)) {
			$this->session->sess_destroy();
			session_destroy();
			$this->output->set_output(json_encode("save_succeeded"));
		} else {
			$this->output->set_output(json_encode("save_failed"));
		}
		
		//echo "<pre>";
		//print_r($constants_file);
		//return ;
	}
	
	function core_setting_submit() {
		
		
		////////////////////读取修改database配置文件////////////////////
		$db_config_file = read_file(DATABASE_CONFIG_FILE);
		
		$core_setting_array = $this->input->post("core_setting_array");
		
		$hostname = "/.db\['default'\]\['hostname'\] = '(.*?)';/";
		$username = "/.db\['default'\]\['username'\] = '(.*?)';/";
		$password = "/.db\['default'\]\['password'\] = '(.*?)';/";
		$database = "/.db\['default'\]\['database'\] = '(.*?)';/";
		$dbdriver = "/.db\['default'\]\['dbdriver'\] = '(.*?)';/";
		
		if ($core_setting_array[1] != $hostname) {
			$db_config_file = preg_replace($hostname, "\$db['default']['hostname'] = '" . $core_setting_array[1] . "';", $db_config_file);
		}
		if ($core_setting_array[2] != $username) {
			$db_config_file = preg_replace($username, "\$db['default']['username'] = '" . $core_setting_array[2] . "';", $db_config_file);
		}
		if ($core_setting_array[3] != $password) {
			$db_config_file = preg_replace($password, "\$db['default']['password'] = '" . $core_setting_array[3] . "';", $db_config_file);
		}
		if ($core_setting_array[4] != $database) {
			$db_config_file = preg_replace($database, "\$db['default']['database'] = '" . $core_setting_array[4] . "';", $db_config_file);
		}
		if ($core_setting_array[5] != $dbdriver) {
			$db_config_file = preg_replace($dbdriver, "\$db['default']['dbdriver'] = '" . $core_setting_array[5] . "';", $db_config_file);
		}
		
		////////////////////同时读取修改全文检索的数据库配置文件////////////////////
		$csft_file = read_file(CORESEEK_CONFIG_FILE);
		
		$sql_host = "/sql_host = (.*)/";
		$sql_user = "/sql_user = (.*)/";
		$sql_pass = "/sql_pass = (.*)/";
		$sql_db = "/sql_db = (.*)/";
		$type = "/type = (.*)/";
		
		if ($core_setting_array[1] != $sql_host) {
			$csft_file = preg_replace($sql_host, "sql_host = " . $core_setting_array[1], $csft_file);
		}
		if ($core_setting_array[2] != $sql_user) {
			$csft_file = preg_replace($sql_user, "sql_user = " . $core_setting_array[2], $csft_file);
		}
		if ($core_setting_array[3] != $sql_pass) {
			$csft_file = preg_replace($sql_pass, "sql_pass = " . $core_setting_array[3], $csft_file);
		}
		if ($core_setting_array[4] != $sql_db) {
			$csft_file = preg_replace($sql_db, "sql_db = " . $core_setting_array[4], $csft_file);
		}
		if ($core_setting_array[5] != $type) {
			$csft_file = preg_replace($type, "type = " . $core_setting_array[5], $csft_file);
		}
		
		////////////////////读取修改config配置文件////////////////////
		$config_config_file = read_file(MAIN_CONFIG_FILE);
		
		$encryption_key = "/.config\['encryption_key'\] = '(.*?)';/";
		
		if ($core_setting_array[6] != $encryption_key) {
			$config_config_file = preg_replace($encryption_key, "\$config['encryption_key'] = '" . $core_setting_array[6] . "';", $config_config_file);
		}
		
		////////////////////将所有配置写入对应的配置文件////////////////////
		if (write_file(DATABASE_CONFIG_FILE, $db_config_file) && write_file(CORESEEK_CONFIG_FILE, $csft_file) && write_file(MAIN_CONFIG_FILE, $config_config_file)) {
			$this->output->set_output(json_encode("save_succeeded"));
		} else {
			$this->output->set_output(json_encode("save_failed"));
		}
		
		//echo "<pre>";
		//print_r($db_config_file);
		//return ;
	}
}